What is node-int64?
The node-int64 npm package is used for handling 64-bit integer values in Node.js. It provides functionality to represent, manipulate, and convert 64-bit integers, which are not natively supported in JavaScript due to its use of IEEE 754 floating-point numbers for all numeric values.
What are node-int64's main functionalities?
Creating 64-bit integers
This feature allows the creation of 64-bit integer instances from various inputs such as number, string, or buffer.
const Int64 = require('node-int64');
const int64 = new Int64('0x1234567890abcdef');
Reading 64-bit integers from buffers
This feature enables reading 64-bit integers directly from buffers, which is useful when dealing with binary data streams.
const Int64 = require('node-int64');
const buffer = Buffer.from('1234567890abcdef', 'hex');
const int64 = new Int64(buffer);
Converting 64-bit integers to strings
This feature allows the conversion of 64-bit integers to their string representation, which can be displayed or stored as text.
const Int64 = require('node-int64');
const int64 = new Int64('0x1234567890abcdef');
const str = int64.toString();
Converting 64-bit integers to native JavaScript numbers
This feature provides the ability to convert 64-bit integers to native JavaScript numbers, with the caveat that precision might be lost for large values.
const Int64 = require('node-int64');
const int64 = new Int64('0x1234567890abcdef');
const num = int64.toNumber();
Other packages similar to node-int64
bignum
The 'bignum' package provides arbitrary-precision arithmetic operations. It is more versatile than 'node-int64' as it can handle numbers of any size, but it might be slower for operations that only require 64-bit precision.
long
The 'long' package also deals with 64-bit integers and provides a rich set of arithmetic and comparison operations. It is similar to 'node-int64' but has a more extensive API for working with 64-bit integers.
bigint-buffer
The 'bigint-buffer' package is designed to work with Node.js's native BigInt type, providing buffer conversion utilities. It is a modern alternative to 'node-int64' that leverages the BigInt type introduced in newer versions of JavaScript.
JavaScript Numbers are represented as IEEE 754 double-precision floats. Unfortunately, this means they lose integer precision for values beyond +/- 2^^53. For projects that need to accurately handle 64-bit ints, such as node-thrift, a performant, Number-like class is needed. Int64 is that class.
Int64 instances look and feel much like JS-native Numbers. By way of example ...
> (0x123456789).toString(16)
'123456789'
> (0x123456789abcdef0).toString(16)
'123456789abcdf00'
> Int64 = require('node-int64')
> x = new Int64(0x123456789)
[Int64 value:4886718345 octets:00 00 00 01 23 45 67 89]
> y = new Int64('123456789abcdef0')
[Int64 value:Infinity octets:12 34 56 78 9a bc de f0]
> x + 1
4886718346
> y + 1
Infinity
> 'value: ' + x
'value: 4886718345'
> 'value: ' + y
'value: Infinity'
> x.toString(2)
'100100011010001010110011110001001'
> y.toString(2)
'Infinity'
> isFinite(x)
true
> isFinite(y)
false
> x.toOctetString()
'0000000123456789'
> y.toOctetString()
'123456789abcdef0'
> new Int64(0x12345678, 0x9abcdef0)
[Int64 value:Infinity octets:12 34 56 78 9a bc de f0]
> new Int64(new Buffer([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0]))
[Int64 value:Infinity octets:12 34 56 78 9a bc de f0]
> new Int64(new Buffer([0,0,0,0,0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0]), 4)
[Int64 value:Infinity octets:12 34 56 78 9a bc de f0]
> new Int64(new Buffer([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0])).toBuffer()
<Buffer 12 34 56 78 9a bc de f0>
> var buf = new Buffer(1024);
> new Int64(new Buffer([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0])).copy(buf, 512);